#! /usr/bin/env python

import roslib; roslib.load_manifest('storage')
import rospy
import sqlite3
import actionlib
import std_msgs
import os
from storage.msg import *

pub = rospy.Publisher("android_request",android)
def addPerson(target):
	conn2 = sqlite3.connect('../ros_workspace/storage/storageDB')
	c2 = conn2.cursor()
	img=(target[0],target[1])
	c2.execute('insert into images values (NULL,?,?)', img)
	imageID=c2.lastrowid
	for i in range(int(target[2])):
		person=(imageID,target[3+i*2],target[4+i*2])
		c2.execute('insert into mainBio values (NULL,?,?,?)',person)
	conn2.commit()
	c2.close()
	path=target[1]  
	dirList=os.listdir(path)
	mesg=[str(imageID),path]
	for fname in dirList:
		rospy.loginfo(os.stat(path+"/"+fname).st_size)
		if os.stat(path+"/"+fname).st_size>40000:
			os.remove(path+"/"+fname)
	pub.publish(android(mesg))
	rospy.loginfo("this happened")

def updateEyeDist(record):
	rospy.loginfo("updating eyedist")
	conn3 = sqlite3.connect('../ros_workspace/storage/storageDB')
	c3 = conn3.cursor()
	rospy.loginfo("start")
	for string in record:
		rospy.loginfo(string)
	eyeDist=float(record[2])
	width=float(record[5])
	ratio=round(eyeDist/width,4)
	entry=record
	entry.insert(7,str(ratio))
	for string in entry:
		rospy.loginfo(string)
	c3.execute('insert into androidBio values (NULL,?,NULL,?,?,?,?,?,?,?,?)',entry)
	rospy.loginfo("end")
	conn3.commit()
	c3.close() 

class storageServer:
  def __init__(self):
    self.server = actionlib.SimpleActionServer("addPerson", addPersonAction, self.execute, False)
    self.server.start()

  def execute(self,goal):
    addPerson(goal.target)
    #once we have reached our target we tell the client that we succeeded
    self.server.set_succeeded()
    
class eyeDistServer:
  def __init__(self):
	  self.eyeServer = actionlib.SimpleActionServer("storage_msg", storage_msgAction, self.execute, False)
	  self.eyeServer.start()
	  
  def execute(self,goal):
	  updateEyeDist(goal.record)
	  #rospy.loginfo(goal.record[0])
	  self.eyeServer.set_succeeded()

if __name__ == '__main__':
	#NOTE: Gotta Make the node executable : chmod +x nodes/storageServer.py
	rospy.init_node("storageServer")
	
	conn = sqlite3.connect('../ros_workspace/storage/storageDB')
	c = conn.cursor()

	# Create table
	c.execute("""create table if not exists androidBio(faceID integer primary key autoincrement, imageID integer,personId int,confidence real, eyeDistance real, midX real, midY real,imgWidth int, imgHeight int,eyeDistRatio real, URL string)""")
	c.execute("""create table if not exists peopleDictionary(name string, eyeDistance real,  height real, shirtColor string)""")
	c.execute("""create table if not exists mainBio(bioID integer primary key autoincrement, imageID integer,height real, shirtColor string)""")
	c.execute("""create table if not exists images(imageID integer primary key autoincrement, url string, facesPath string)""")
	# Save (commit) the changes
	conn.commit()
	c.close()
	eyeServer=eyeDistServer()
	server=storageServer()
	rospy.spin()
